package com.axdoc.wallet.service.impl;

import java.sql.Timestamp;
import java.util.List;

import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.MediaType;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.axdoc.common.constant.WalletConstant;
import com.axdoc.common.utils.SnowflakeIdWorker;
import com.axdoc.wallet.dao.IPatientWalletDao;
import com.axdoc.wallet.entity.PatientActivitySubWallet;
import com.axdoc.wallet.entity.PatientActivitySubWalletRecords;
import com.axdoc.wallet.entity.PatientBalanceSubWallet;
import com.axdoc.wallet.entity.PatientBalanceSubWalletRecords;
import com.axdoc.wallet.entity.PatientCouponSubWallet;
import com.axdoc.wallet.entity.PatientCouponSubWalletRecords;
import com.axdoc.wallet.entity.PatientVipSubWallet;
import com.axdoc.wallet.entity.PatientVipSubWalletRecords;
import com.axdoc.wallet.entity.PatientWallet;
import com.axdoc.wallet.exceptions.WalletBizException;
import com.axdoc.wallet.service.PatientWalletService;

@Service("patientWalletService")
@Path("patient")
@Produces({ "application/json; charset=UTF-8", "text/xml; charset=UTF-8" })
public class PatientWalletServiceImpl implements PatientWalletService {
	
	public static Logger logger = LoggerFactory.getLogger("time");

	@Autowired
	private IPatientWalletDao patientWalletDao;

	@GET
	@POST
	@Path("/createPatientWallet/")
	@Transactional(rollbackFor = Exception.class)
	public boolean createPatientWallet(@QueryParam("aid") String aid, @QueryParam("walletType") int walletType)
			throws WalletBizException, Exception {
		long startTime = System.currentTimeMillis();
		boolean result = false;
		if (StringUtils.isBlank(aid)) {
			throw new WalletBizException(WalletBizException.MISSING_ARGS, "缺少参数对象aid");
		}
		if (0 != walletType && 1 != walletType) {
			throw new WalletBizException(WalletBizException.NOT_EXSIT, "参数对象walletType值不正确");
		}
		String walletNo = String.valueOf(SnowflakeIdWorker.getInstance().nextId());
		if (StringUtils.isBlank(walletNo)) {
			throw new WalletBizException(WalletBizException.ERROR, "患者账户编号生成失败！不能为空！");
		}
		// 添加患者账户
		PatientWallet pw = new PatientWallet();
		pw.setAid(aid);
		pw.setWalletNo(walletNo);
		pw.setWalletType(walletType);
		pw.setpNo("");
		result = patientWalletDao.addPatientWallet(pw);
		if (!result) {
			throw new WalletBizException(WalletBizException.ERROR, "患者账户添加失败！");
		}
		// 添加患者余额子账户
		PatientBalanceSubWallet balance = new PatientBalanceSubWallet();
		balance.setWalletNo(walletNo + "01");
		balance.setmWalletNo(walletNo);
		balance.setPayPassword("");
		balance.setIsAllowedPay(WalletConstant.WALLET_IS_ALLOW_PAY);
		balance.setIsAllowedRecharge(WalletConstant.WALLET_IS_ALLOW_RECHARGE);
		balance.setIsAllowedWithdrawal(WalletConstant.WALLET_IS_NOT_ALLOW_WITHDRAWL);
		balance.setState(WalletConstant.WALLET_STATUS_ALIVE);
		balance.setBalance(0);
		balance.setWithdrawalBalanceCan(0);
		balance.setWithdrawalBalanceWait(0);
		balance.setForzenBalance(0);
		balance.setAid(aid);
		result = patientWalletDao.addPatientBalanceSubWallet(balance);
		if (!result) {
			throw new WalletBizException(WalletBizException.ERROR, "患者余额子账户添加失败！");
		}
		// 添加患者券子账户
		PatientCouponSubWallet coupon = new PatientCouponSubWallet();
		coupon.setWalletNo(walletNo + "02");
		coupon.setmWalletNo(walletNo);
		coupon.setCouponNumber(0);
		coupon.setState(WalletConstant.WALLET_STATUS_ALIVE);
		coupon.setAid(aid);
		result = patientWalletDao.addPatientCouponSubWallet(coupon);
		if (!result) {
			throw new WalletBizException(WalletBizException.ERROR, "患者券子账户添加失败！");
		}
		// 添加患者vip子账户
		PatientVipSubWallet vip = new PatientVipSubWallet();
		vip.setWalletNo(walletNo + "03");
		vip.setmWalletNo(walletNo);
		vip.setServiceNumber(0);
		vip.setServiceId("");
		vip.setState(WalletConstant.WALLET_STATUS_ALIVE);
		vip.setAid(aid);
		result = patientWalletDao.addPatientVipSubWallet(vip);
		if (!result) {
			throw new WalletBizException(WalletBizException.ERROR, "患者vip子账户添加失败！");
		}
		// 添加患者活动子账户
		PatientActivitySubWallet activity = new PatientActivitySubWallet();
		activity.setWalletNo(walletNo + "04");
		activity.setmWalletNo(walletNo);
		activity.setActivityNumber(0);
		activity.setState(WalletConstant.WALLET_STATUS_ALIVE);
		activity.setAid(aid);
		result = patientWalletDao.addPatientActivitySubWallet(activity);
		if (!result) {
			throw new WalletBizException(WalletBizException.ERROR, "患者活动子账户添加失败！");
		}
		long endTime = System.currentTimeMillis();
		logger.debug("createPatientWallet aid="+aid+",time = "+(endTime-startTime));
		return result;
	}

	@GET
	@POST
	@Path("/queryPatientWalletByAid/")
	public PatientWallet queryPatientWalletByAid(@QueryParam("aid") String aid) throws WalletBizException, Exception {
		if (StringUtils.isBlank(aid)) {
			throw new WalletBizException(WalletBizException.MISSING_ARGS, "缺少参数对象aid");
		}
		return patientWalletDao.queryPatientWalletByAid(aid);
	}

	@GET
	@POST
	@Path("/queryPatientBalanceSubWalletByAid/")
	public PatientBalanceSubWallet queryPatientBalanceSubWalletByAid(@QueryParam("aid") String aid)
			throws WalletBizException, Exception {
		if (StringUtils.isBlank(aid)) {
			throw new WalletBizException(WalletBizException.MISSING_ARGS, "缺少参数对象aid");
		}
		return patientWalletDao.queryPatientBalanceSubWalletByAid(aid);
	}

	@GET
	@POST
	@Path("/queryPatientActivitySubWalletByAid/")
	public PatientActivitySubWallet queryPatientActivitySubWalletByAid(@QueryParam("aid") String aid)
			throws WalletBizException, Exception {
		if (StringUtils.isBlank(aid)) {
			throw new WalletBizException(WalletBizException.MISSING_ARGS, "缺少参数对象aid");
		}
		return patientWalletDao.queryPatientActivitySubWalletByAid(aid);
	}

	@GET
	@POST
	@Path("/queryPatientCouponSubWalletByAid/")
	public PatientCouponSubWallet queryPatientCouponSubWalletByAid(@QueryParam("aid") String aid)
			throws WalletBizException, Exception {
		if (StringUtils.isBlank(aid)) {
			throw new WalletBizException(WalletBizException.MISSING_ARGS, "缺少参数对象aid");
		}
		return patientWalletDao.queryPatientCouponSubWalletByAid(aid);
	}

	@GET
	@POST
	@Path("/queryPatientVipSubWalletByAid/")
	public PatientVipSubWallet queryPatientVipSubWalletByAid(@QueryParam("aid") String aid)
			throws WalletBizException, Exception {
		if (StringUtils.isBlank(aid)) {
			throw new WalletBizException(WalletBizException.MISSING_ARGS, "缺少参数对象aid");
		}
		return patientWalletDao.queryPatientVipSubWalletByAid(aid);
	}

	@POST
	@Path("/updatePatientBalanceSubWallet/")
	@Consumes({ MediaType.APPLICATION_JSON })
	public boolean updatePatientBalanceSubWallet(PatientBalanceSubWallet patientBalanceSubWallet)
			throws WalletBizException, Exception {
		if (null == patientBalanceSubWallet || 0 == patientBalanceSubWallet.getId()) {
			throw new WalletBizException(WalletBizException.MISSING_ARGS, "缺少参数对象id");
		}
		return patientWalletDao.updatePatientBalanceSubWallet(patientBalanceSubWallet);
	}

	@POST
	@Path("/updatePatientActivitySubWallet/")
	@Consumes({ MediaType.APPLICATION_JSON })
	public boolean updatePatientActivitySubWallet(PatientActivitySubWallet patientActivitySubWallet)
			throws WalletBizException, Exception {
		if (null == patientActivitySubWallet || 0 == patientActivitySubWallet.getId()) {
			throw new WalletBizException(WalletBizException.MISSING_ARGS, "缺少参数对象id");
		}
		return patientWalletDao.updatePatientActivitySubWallet(patientActivitySubWallet);
	}

	@POST
	@Path("/updatePatientCouponSubWallet/")
	@Consumes({ MediaType.APPLICATION_JSON })
	public boolean updatePatientCouponSubWallet(PatientCouponSubWallet patientCouponSubWallet)
			throws WalletBizException, Exception {
		if (null == patientCouponSubWallet || 0 == patientCouponSubWallet.getId()) {
			throw new WalletBizException(WalletBizException.MISSING_ARGS, "缺少参数对象id");
		}
		return patientWalletDao.updatePatientCouponSubWallet(patientCouponSubWallet);
	}

	@POST
	@Path("/updatePatientVipSubWallet/")
	@Consumes({ MediaType.APPLICATION_JSON })
	public boolean updatePatientVipSubWallet(PatientVipSubWallet patientVipSubWallet)
			throws WalletBizException, Exception {
		if (null == patientVipSubWallet || 0 == patientVipSubWallet.getId()) {
			throw new WalletBizException(WalletBizException.MISSING_ARGS, "缺少参数对象id");
		}
		return patientWalletDao.updatePatientVipSubWallet(patientVipSubWallet);
	}

	@GET
	@POST
	@Path("/queryBalanceRecords/")
	public List<PatientBalanceSubWalletRecords> queryBalanceRecords(@QueryParam("tradeNo") String tradeNo,
			@QueryParam("startTime") String startTime, @QueryParam("endTime") String endTime)
			throws WalletBizException, Exception {
		if (StringUtils.isBlank(tradeNo)) {
			throw new WalletBizException(WalletBizException.MISSING_ARGS, "缺少参数对象tradeNo");
		}
		if (StringUtils.isBlank(startTime)) {
			throw new WalletBizException(WalletBizException.MISSING_ARGS, "缺少参数对象startTime");
		}
		if (StringUtils.isBlank(endTime)) {
			throw new WalletBizException(WalletBizException.MISSING_ARGS, "缺少参数对象endTime");
		}
		Timestamp st = Timestamp.valueOf(startTime);
		Timestamp et = Timestamp.valueOf(endTime);
		return patientWalletDao.queryBalanceRecords(tradeNo, st, et);
	}

	@GET
	@POST
	@Path("/queryCouponRecords/")
	public List<PatientCouponSubWalletRecords> queryCouponRecords(@QueryParam("tradeNo") String tradeNo,
			@QueryParam("startTime") String startTime, @QueryParam("endTime") String endTime)
			throws WalletBizException, Exception {
		if (StringUtils.isBlank(tradeNo)) {
			throw new WalletBizException(WalletBizException.MISSING_ARGS, "缺少参数对象tradeNo");
		}
		if (StringUtils.isBlank(startTime)) {
			throw new WalletBizException(WalletBizException.MISSING_ARGS, "缺少参数对象startTime");
		}
		if (StringUtils.isBlank(endTime)) {
			throw new WalletBizException(WalletBizException.MISSING_ARGS, "缺少参数对象endTime");
		}
		Timestamp st = Timestamp.valueOf(startTime);
		Timestamp et = Timestamp.valueOf(endTime);
		return patientWalletDao.queryCouponRecords(tradeNo, st, et);
	}

	@GET
	@POST
	@Path("/queryActivityRecords/")
	public List<PatientActivitySubWalletRecords> queryActivityRecords(@QueryParam("tradeNo") String tradeNo,
			@QueryParam("startTime") String startTime, @QueryParam("endTime") String endTime)
			throws WalletBizException, Exception {
		if (StringUtils.isBlank(tradeNo)) {
			throw new WalletBizException(WalletBizException.MISSING_ARGS, "缺少参数对象tradeNo");
		}
		if (StringUtils.isBlank(startTime)) {
			throw new WalletBizException(WalletBizException.MISSING_ARGS, "缺少参数对象startTime");
		}
		if (StringUtils.isBlank(endTime)) {
			throw new WalletBizException(WalletBizException.MISSING_ARGS, "缺少参数对象endTime");
		}
		Timestamp st = Timestamp.valueOf(startTime);
		Timestamp et = Timestamp.valueOf(endTime);
		return patientWalletDao.queryActivityRecords(tradeNo, st, et);
	}

	@GET
	@POST
	@Path("/queryVipRecords/")
	public List<PatientVipSubWalletRecords> queryVipRecords(@QueryParam("tradeNo") String tradeNo,
			@QueryParam("startTime") String startTime, @QueryParam("endTime") String endTime)
			throws WalletBizException, Exception {
		if (StringUtils.isBlank(tradeNo)) {
			throw new WalletBizException(WalletBizException.MISSING_ARGS, "缺少参数对象tradeNo");
		}
		if (StringUtils.isBlank(startTime)) {
			throw new WalletBizException(WalletBizException.MISSING_ARGS, "缺少参数对象startTime");
		}
		if (StringUtils.isBlank(endTime)) {
			throw new WalletBizException(WalletBizException.MISSING_ARGS, "缺少参数对象endTime");
		}
		Timestamp st = Timestamp.valueOf(startTime);
		Timestamp et = Timestamp.valueOf(endTime);
		return patientWalletDao.queryVipRecords(tradeNo, st, et);
	}

}
